package com.zhaolin.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Splitter;
import com.zhaolin.entity.Admin;
import com.zhaolin.mapper.AdminDao;
import com.zhaolin.service.adminService;
import com.zhaolin.utils.BeanMapUtils;
import com.zhaolin.utils.MapParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service
@Transactional
public class AdminServiceImpl implements adminService {
    @Autowired
    AdminDao adminDao;


    @Override
    public int create(Admin pi) {
        int num = adminDao.create(pi);
        return num;
    }

    @Override
    public int delete(Integer id) {
        return adminDao.delete(MapParameter.getInstance().addId(id).getMap());
    }

    //批量删除
    @Override
    public int deleteBatch(String ids) {
        int flag = 0;
        List<String>list = Splitter.on(",").splitToList(ids);
        for (String s : list){
            adminDao.delete(MapParameter.getInstance().addId(Integer.parseInt(s)).getMap());
            flag++;
        }

        return flag;
    }

    @Override
    public int update(Admin admin) {
       /* Map<String,Object>map = BeanMapUtils.beanToMapForUpdate(admin);*/
        Map<String,Object>map =MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(admin)).add("id", admin.getId()).getMap();
        return adminDao.update(map);
    }

    @Override
    public List<Admin> query(Admin admin) {
        Page<Object> objects = PageHelper.startPage(admin.getPage(), admin.getLimit());
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(admin)).getMap();
        return adminDao.query(map);
    }

    @Override
    public Admin detail(Integer id) {
        Admin id1 = adminDao.detail(MapParameter.getInstance().add("id", id).getMap());
        return id1;
    }


    @Override
    public int count(Admin admin) {
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMap(admin)).getMap();
        return adminDao.count(map);
    }

    @Override
    public Admin login(String account, String password) {
        return adminDao.login(account,password);
    }
}
